package com.ce.pms.customer.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.hibernate.Query;

import com.ce.pms.base.dao.impl.PmsBaseDaoImpl;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.pms.customer.dao.IClientRHouseDao;
import com.ce.pms.customer.model.ClientDO;
import com.ce.pms.customer.model.ClientRHouseDO;
import com.ce.pms.customer.model.ParticipantDO;
import com.ce.pms.house.model.HouseDomain;
import com.ce.util.BlankUtil;

public class ClientRHouseDaoImpl extends PmsBaseDaoImpl<ClientRHouseDO> implements IClientRHouseDao{

	/**
	 * 功能描述: 根据房间id得到所有有效的业主实体 <br>
	 * @param houseId
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-8-15  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public List<ParticipantDO> getAllOwnerByHouseId(Long houseId){
		SysrefcodeDO clientType = this.sysrefcodeDao.getSysrefCodesByCode(
									SysrefcodeDO.CS_CLIENT_CLIENT_TYPE_CODE,
									SysrefcodeDO.CLIENT_TYPE_OWNER);
		List<ClientRHouseDO> list = this.getClientRHouseListByHouseIdAndClientType(houseId, clientType.getId());

		List<ParticipantDO> resList = dealParticipantList(list);
		return resList;
	}

	/**
	 * 功能描述: 根据房间id得到所有的业主实体 <br>
	 * @param houseId
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-8-15  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public List<ParticipantDO> getAllOwnerByHouseId(Long houseId,Long proRightId){
		SysrefcodeDO clientType = this.sysrefcodeDao.getSysrefCodesByCode(SysrefcodeDO.CS_CLIENT_CLIENT_TYPE_CODE, SysrefcodeDO.CLIENT_TYPE_OWNER);
		List<ClientRHouseDO> list = this.getClientRHouseListByHouseIdAndProRight(houseId,proRightId,clientType.getId());

		List<ParticipantDO> resList = dealParticipantList(list);
		return resList;
	}

	/**
	 * 功能描述: 根据房间id得到所有的业主名称列表 <br>
	 * @param houseId
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-8-15  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public List<String> getAllOwnerNameByHouseId(Long houseId){
		List<ParticipantDO> pList = this.getAllOwnerByHouseId(houseId);
		List<String> resList = new ArrayList<String>();
		if (!BlankUtil.isBlank(pList)) {
			for (ParticipantDO participantDO : pList) {
				resList.add(participantDO.getParticipantName());
			}
		}
		return resList;
	}

	/**
	 * Function: 根据房间id找出其业主姓名,若有多个用,号隔开
	 * @param houseId
	 * @return String
	 * Create author:胡煦
	 * Create on:2008-9-9
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public String getOwnerNameBy(Long houseId)
	{
		StringBuffer sb = new StringBuffer("");
		List<String> pList = this.getAllOwnerNameByHouseId(houseId);
		if(!BlankUtil.isBlank(pList))
		{
			for(String string : pList)
			{
				sb.append(string).append(",");
			}
			sb = sb.deleteCharAt(sb.length()-1);
		}
		return sb.toString();
	}


	/**
	 * 功能描述: 取得一组房屋和给定客户类型的客户房屋关系列表 <br>
	 *@param houses
	 *@param cType
	 *@return
	 * @author Adama Yue
	 * @CreateDate:上午11:39:19
	 */
	@SuppressWarnings("unchecked")
	public List<ClientRHouseDO> getByHousesAndClientType(Set<HouseDomain> houses, SysrefcodeDO cType) {
		StringBuilder queryStr = new StringBuilder();

		queryStr.append("FROM ClientRHouseDO crh WHERE crh.house.id in ( ");
		for (HouseDomain house : houses) {
			queryStr.append(house.getKeyword()).append(",");
		}
		queryStr.setLength(queryStr.length() - 1 );
		queryStr.append(") AND crh.client.clientType.id=").append(cType.getId()).append(" AND crh.validity=1 ");

		Query query = super.createQuery(queryStr.toString());

		return query.list();

	}

	/**
	 * Function:根据房屋id和客户id查询相应的客户房屋关系实体
	 * @param
	 * @return
	 * Create author:xuhaidui
	 * Create on:2008-6-6
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public ClientRHouseDO getClientRHouseByHouseAndClient(Long houseId,Long clientId){
		ClientRHouseDO clientRHouseDO = null;
		StringBuilder queryStr = new StringBuilder();
		queryStr.append("FROM ClientRHouseDO crh WHERE crh.house.id=? AND crh.client.participantRoleId=? AND crh.validity=1 ");

		Query query = super.createQuery(queryStr.toString(), houseId,clientId);

		List list = query.list();

		if (list.size()>0) {
			clientRHouseDO = (ClientRHouseDO)list.get(0);
		}

		return clientRHouseDO;
	}

	/**
	 * Function:根据房屋id和参与人id查询相应的客户房屋关系实体
	 * @param
	 * @return
	 * Create author:xuhaidui
	 * Create on:2008-6-6
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public ClientRHouseDO getClientRHouseByHouseAndParticipantId(Long houseId,Long participantId){
		ClientRHouseDO clientRHouseDO = null;
		StringBuilder queryStr = new StringBuilder();
		queryStr.append("FROM ClientRHouseDO crh WHERE crh.house.id=? AND crh.client.participant.participantId=? AND crh.validity=1 ");

		Query query = super.createQuery(queryStr.toString(), houseId,participantId);

		List list = query.list();

		if (list.size()>0) {
			clientRHouseDO = (ClientRHouseDO)list.get(0);
		}

		return clientRHouseDO;
	}

	/**
	 *
	 * 功能描述: 通过客户id得到客户房屋关系列表
	 *
	 * @author 付湘辉
	 * Created on:2008-6-11
	 *
	 * 修改原因：
	 * @param clientId
	 * @return List 返回结果
	 */
	@SuppressWarnings("unchecked")
	public List<ClientRHouseDO> getClientRHouseListByClientId(Long clientId){
		List<ClientRHouseDO> list = null;
		StringBuilder queryStr = new StringBuilder();

		queryStr.append("FROM ClientRHouseDO crh WHERE crh.client.participantRoleId=? AND crh.validity='1' ");

		Query query = super.createQuery(queryStr.toString(), clientId);

		list = query.list();

		return list;
	}

	/**
	 * 功能描述: 通过房屋id得到其对应的客户房屋关系列表 <br>
	 * @param houseId
	 * @return
	 *
	 * Create author: <br>
	 * Create on:2008-7-28  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	@SuppressWarnings("unchecked")
	public List<ClientRHouseDO> getClientRHouseListByHouseId(Long houseId) {

		List<ClientRHouseDO> list = null;
		StringBuilder queryStr = new StringBuilder();

		queryStr.append("FROM ClientRHouseDO crh WHERE crh.house.id=? AND crh.validity=1 ");

		Query query = super.createQuery(queryStr.toString(), houseId);

		list = query.list();

		return list;
	}

	/**
	 * 功能描述: 通过房屋id和客户类型id得到其对应的客户房屋关系列表 <br>
	 * @param houseId
	 * @return
	 *
	 * Create author: <br>
	 * Create on:2008-7-28  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	@SuppressWarnings("unchecked")
	public List<ClientRHouseDO> getClientRHouseListByHouseIdAndClientType(Long houseId,Long clientTypeId) {

		List<ClientRHouseDO> list = null;
		StringBuilder queryStr = new StringBuilder();

		queryStr.append("FROM ClientRHouseDO crh WHERE crh.house.id=? AND crh.client.clientType.id=? AND crh.validity=1 ");

		Query query = super.createQuery(queryStr.toString(), houseId,clientTypeId);

		list = query.list();

		return list;
	}
	/**
	 * 功能描述: 通过房屋id和客户类型id得到其对应的客户房屋关系列表 <br>
	 * 入住前的验证使用,居住管理入住前,需要验证相关客户都已搬出
	 * @param houseId
	 * @return
	 *
	 * Create author: <br>
	 * Create on:2008-7-28  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	@SuppressWarnings("unchecked")
	public List<ClientRHouseDO> getClientRHouseListByHouseIdAndClientType(String houseIds,Long clientTypeId){
		StringBuffer queryStr = new StringBuffer("FROM ClientRHouseDO crh ");
				queryStr.append(" WHERE crh.house.id IN(" ).append(houseIds).append(") ");
				queryStr.append(" AND crh.client.clientType.id=? ");
				queryStr.append(" AND crh.inDate IS NOT NULL ");
				queryStr.append(" AND crh.outDate IS NULL ");
				queryStr.append(" AND crh.validity=1 ");
		Query query = super.createQuery(queryStr.toString(),clientTypeId);
		List<ClientRHouseDO> clientRHouseList = query.list();
		return clientRHouseList;
	}

	/**
	 * 功能描述: 通过房屋id和产权id和客户类型id得到其对应的客户房屋关系列表 <br>
	 * @param houseId　房屋id
	 * @param proRightId　产权id
	 * @param clientTypeId　客户类型id
	 * @return
	 *
	 * Create author: <br>
	 * Create on:2008-7-28  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	@SuppressWarnings("unchecked")
	public List<ClientRHouseDO> getClientRHouseListByHouseIdAndProRight(Long houseId,Long proRightId,Long clientTypeId) {

		List<ClientRHouseDO> list = null;
		StringBuilder queryStr = new StringBuilder();

		queryStr.append("FROM ClientRHouseDO crh WHERE crh.house.id=? AND crh.proRight.id=? AND crh.client.clientType.id=? ");

		Query query = super.createQuery(queryStr.toString(), houseId,proRightId,clientTypeId);

		list = query.list();

		return list;
	}

	/**
	 * 功能描述: 通过房屋id和产权id和客户类型value得到其对应的有效客户房屋关系实体 <br>
	 * @param houseId　房屋id
	 * @param proRightId　产权id
	 * @param clientType　客户类型value
	 * @return List<ClientRHouseDO>
	 * Create author: 胡煦<br>
	 * Create on:2008-10-14  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	@SuppressWarnings("unchecked")
	public List<ClientRHouseDO> getClientRHouseListByHouseIdAndProRight(Long houseId,Long proRightId,String clientType) {

		String queryStr = "FROM ClientRHouseDO crh WHERE crh.house.id=? AND crh.proRight.id=? AND crh.validity = 1 AND crh.client.clientType.value=? ";

		Query query = super.createQuery(queryStr, houseId,proRightId,clientType);

		return query.list();
	}

	/** 功能描述: 得到指定客户房屋关系列表中的客户实体，去掉重复的 <br>
	 * @param list
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-9-5  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	*/
	private List<ParticipantDO> dealParticipantList(List<ClientRHouseDO> list) {
		List<ParticipantDO> resList = new ArrayList<ParticipantDO>();

		if (!BlankUtil.isBlank(list)) {
			List<Long> idList = new ArrayList<Long>();
			for (ClientRHouseDO chouseDo : list) {
				ClientDO client = chouseDo.getClient();

				ParticipantDO part = client.getParticipant();
				if (idList.contains(part.getParticipantId())) {
					continue;
				}
				idList.add(part.getParticipantId());
				resList.add(part);
			}
		}
		return resList;
	}
}
